class BannerWebpackPlugin {
    constructor(options) {
        this.options = options
    }

    apply(compiler) {
        compiler.hooks.emit.tapAsync("BannerWebpackPlugin", (compilation, callback) => {
            debugger
            // 1.定义需要提取的文件类型
            const acceptExtensions = ['js', 'css']
            // 2. 过滤出需要提取的文件的文件路径
            const assetPaths = Object.keys(compilation.assets).filter(assetKey => {
                const splitted = assetKey.split('.')
                return acceptExtensions.includes(splitted[splitted.length - 1])
            })

            // 3. 找出需要提取文件的内容
            assetPaths.forEach(assetPath => {
                const asset = compilation.assets[assetPath]
                //修改资源
                const source = `/*
* Author:${this.options.authorName}
*/\n${asset.source()}`

                // 覆盖资源
                compilation.assets[assetPath] = {
                    // 资源内容
                    source() { 
                        return source
                    },
                    // 资源大小
                    size() {
                        return source.length
                    }
                }
            })
            callback()
        })
    }
}

module.exports = BannerWebpackPlugin